package com.zjsos.governmentaffairs.sokettestapplication;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.jaydenxiao.common.commonutils.ToastUtil;

import org.jeromq.ZMQ;

public class ZmqClient {

    private ZMQ.Context reqContext,subContext;
    private ZMQ.Socket reqSocket,subSocket;
    private static ZmqClient client;
    private Handler reqHandler,subHandler;    //子线程的handler，存在于子线程，用于把消息从主线程发往子线程
    private HandlerThread reqThread,subThread;
    public static final int START_REQ=0;
    public static final int START_SUB=1;
    public static final int SEND_REQ_MSG=2;
    public static final int SEND_SUB_MSG=3;
    private String reqUrl;



    public static ZmqClient instance(){
        if (client==null){
            synchronized (ZmqClient.class){
                if (client==null){
                    client=new ZmqClient();
                }
            }
        }
        return client;
    }

    private ZmqClient() {
        init();
    }

    private void init(){
        reqThread=new HandlerThread("REQThread");
        reqThread.start();
        reqHandler=new Handler(reqThread.getLooper()){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case START_REQ:
                        String reqUrl= (String) msg.obj;
                        startREQ(reqUrl);
                        break;
                    case SEND_REQ_MSG:
                        String reqMsg= (String) msg.obj;
                        sendREQMessageToServer(reqMsg);
                        break;

                }
            }
        };

        subThread=new HandlerThread("REQThread");
        subThread.start();
        subHandler=new Handler(subThread.getLooper()){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case START_SUB:
                        String subUrl= (String) msg.obj;
                        startSUB(subUrl);
                        break;
                    case SEND_SUB_MSG:
                        String subMsg= (String) msg.obj;
                        sendSUBMessageToServer(subMsg);
                        break;
                }
            }
        };
    }


    //启动应答模式
    private void startREQ(String tcpUrl) {
        this.reqUrl=tcpUrl;
        if (reqContext!=null&&reqSocket!=null){
            return;
        }
        reqContext=ZMQ.context(1);
        reqSocket=reqContext.socket(ZMQ.REQ);
        reqSocket.setSendTimeOut(1500); // TODO fix hardcoded values
        reqSocket.setReceiveTimeOut(10000);
        reqSocket.connect(tcpUrl);
    }

    //启动订阅模式
    private void startSUB(String subUrl) {
        if (subContext!=null&&subContext!=null){
            return;
        }
        subContext=ZMQ.context(1);
        subSocket=subContext.socket(ZMQ.SUB);
        subSocket.setSendTimeOut(1500); // TODO fix hardcoded values
        subSocket.setReceiveTimeOut(10000);
        subSocket.connect(subUrl);
    }


    private void sendREQMessageToServer(String msg){
        if (!reqSocket.connect(reqUrl)){
            ToastUtil.showShort("连接已断开！");
            return;
        }
        reqSocket.send(msg.getBytes());
        byte[] reply = reqSocket.recv(0);
        if (reply!=null){
            Log.e("test","reqMsg="+new String(reply));
        }
    }

    private void sendSUBMessageToServer(String msg){
        subSocket.subscribe(msg.getBytes());
        while (!reqThread.isInterrupted()){
            byte[] reply = subSocket.recv(0);
            if (reply!=null){
                Log.e("test","subMsg="+new String(reply));
            }
        }
    }



    public void sendREQMessage(int msgType,String msg){
        Message message=reqHandler.obtainMessage();
        message.what=msgType;
        message.obj=msg;
        reqHandler.sendMessageDelayed(message,50);
    }

    public void sendSUBMessage(int msgType,String msg){
        Message message=subHandler.obtainMessage();
        message.what=msgType;
        message.obj=msg;
        subHandler.sendMessageDelayed(message,50);
    }




}
